package cn.com.gridinfo.sooc.core.expand.score.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.score.adapter.MyCountsAdapter;
import cn.com.gridinfo.sooc.core.expand.score.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.score.dao.ScoreDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 积分明细的Fragment
 *
 * @author yingchunli
 */
public class CountsDetailFragment extends BaseSoocFragment {

    View view;//当前界面
    @InjectView(R.id.counts_list)
    UltimateRecyclerView countsList;//列表
    LinearLayoutManager linearLayoutManager;//布局管理器
    MyCountsAdapter adapter;//适配器
    ScoreDao dao;
    String parameter;//从activity传递过来的参数
    private int key = 0;//请求时使用 0为全部，1为收入，2为支出
    int maxLastPositon;//分页最后位置

    public static CountsDetailFragment newInstance(String info) {
        Bundle args = new Bundle();
        CountsDetailFragment fragment = new CountsDetailFragment();
        args.putString("info", info);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.expand_score_counts, null);
        ButterKnife.inject(this, view);
        dao = new ScoreDao(this);
        //获取传递过来的参数值
        parameter = getArguments().getString("info");
        key = Integer.parseInt(parameter);
        //初始化listView
        linearLayoutManager = new LinearLayoutManager(getActivity());
        countsList.setLayoutManager(linearLayoutManager);
        //发送请求  获取列表数据
        PageConst.pageNo = 1;//首页
        dao.getCounts(key, PageConst.pageNo, PageConst.pageSize, CodeConst.GET_COUNTS_FIRST);//获取积分明细
        return view;
    }

    /**
     * 初始化数据
     */
    private void initView() {
        adapter = new MyCountsAdapter(dao.getCountsList(), getActivity());
        countsList.setAdapter(adapter);
        countsList.enableLoadmore();
        //下拉刷新
        countsList.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //下拉刷新
                PageConst.pageNo = 1;//首页
                dao.getCounts(key, PageConst.pageNo, PageConst.pageSize, CodeConst.GET_COUNTS_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        //上拉加载
        countsList.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                //上拉加载
                if (itemsCount > (PageConst.pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    dao.getCounts(key, ++PageConst.pageNo, PageConst.pageSize, CodeConst.GET_COUNTS_PULLUP);
                } else {
                    if (countsList != null)
                        adapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                }
            }
        });
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    //获取列表  首次请求
                    case CodeConst.GET_COUNTS_FIRST:
                        initView();
                        break;
                    // 获取积分明细列表 上拉加载
                    case CodeConst.GET_COUNTS_PULLUP:
                        if (dao.getCountsList() != null && !dao.getCountsList().isEmpty()) {
                            //加载更多
                            adapter.loadmore(dao.getCountsList());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                    //积分明细  下拉请求
                    case CodeConst.GET_COUNTS_PUSHDOWN:
                        countsList.setRefreshing(false);
                        if (dao.getCountsList() != null && !dao.getCountsList().isEmpty()) {
                            countsList.hideEmptyView();
                        } else {
                            countsList.showEmptyView();
                        }
                        adapter.refresh(dao.getCountsList());
                        if (dao.getCountsList().size() > (PageConst.pageSize - 1))
                            //数据大于每页显示的条数,则重新开启加载更多
                            adapter.loadMoreDataView();
                        else {
                            //数据小于当前页的话则不显示加载更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(getActivity(), dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(getActivity(), "请求数据异常");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
